package com.kcnet.qk.controller.addfriend;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

/**
 * Created by Administrator on 2018/7/9.
 * 搜索添加好友
 */

public class AddSearchCTR extends BasePeopleController {


    private static String TAG = "搜索添加好友";

    private int mType;
    private String mClassName;

    private static AddSearchCTR mRongCloudInstance;
    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());
    private String text;


    private boolean isJoinInput,isJoinUser;

    private List<String> names = new ArrayList<>();

    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static AddSearchCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (AddSearchCTR.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new AddSearchCTR();
                }
            }
        }

        return mRongCloudInstance;
    }


    @Override
    public void doTask(AccessibilityEvent event) throws Exception {
        mType = event.getEventType();
        mClassName = (String) event.getClassName();
        if ( mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.LauncherUI)) {
            NLogger.d(TAG,"1.进入微信首页 点击通讯录按钮");

           sleepTime(2000);
            tagTextClick("通讯录");
            sleepTime(1000);
            NLogger.d(TAG,"2.到达通讯录界面 点击 新的朋友");
            tagTextClick("新的朋友");

        } else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.FMessageConversationUI)) {
            NLogger.d(TAG,"3.达到新的朋友界面 点击 添加朋友按钮");
            tagTextClick("添加朋友");
        }
        else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.AddMoreFriendsUI)){
            NLogger.d(TAG,"4.添加朋友界面 点击搜索框");
            RootShell.click(24,158);
            isJoinInput = true;
        }
        else if(isJoinInput && (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.FTSAddFriendUI))) {

            if(currentAddCount == names.size()) {
                currentAddCount = 0;
                stopTask();
                return;
            }
            String name = names.get(currentAddCount);
            NLogger.d(TAG,"5.达到搜索好友输入框界面 输入搜索的姓名"+name);
            RootShell.click(492,56);
            sleepTime(1500);
            inputSayHiSentence(name);
            NLogger.d(TAG,"6.输入完成,确认");
            sleepTime(1000);
            RootShell.click(107,141);
            currentAddCount++;
            isJoinInput = false;
            isJoinUser = true;
        }
        else if(isJoinUser && (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.FTSAddFriendUI))) {
            if(findTextClick("该用户不存在")){
                isJoinUser =false;
                isJoinInput = true;
                if(currentAddCount == names.size()) {
                    currentAddCount = 0;
                    stopTask();
                    return;
                }
                String name = names.get(currentAddCount);
                NLogger.d(TAG,"5.达到搜索好友输入框界面 输入搜索的姓名"+name);
                RootShell.click(492,56);
                sleepTime(1500);
                inputSayHiSentence(name);
                NLogger.d(TAG,"6.输入完成,确认");
                sleepTime(1000);
                RootShell.click(107,141);
                currentAddCount++;
                isJoinInput = false;
                isJoinUser = true;
            }
        }

        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.ContactInfoUI)) {
            NLogger.d(TAG,"7.达到搜索的人的信息界面 ");
            isJoinInput = true;
            isJoinUser  = false;
            peoPleInfoUI();
        } else if ( mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.SayHiWithSnsPermissionUI)) {
            NLogger.d(TAG,"9.输入好友验证信息 添加好友");
            sleepTime(1000);
            inputSayHiSentence(text);
            sleepTime(1000);
            RootShell.click(450,38);
        }
    }

    private void peoPleInfoUI() {
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            //如果可以发消息，说明已经加为好友
            List<AccessibilityNodeInfo> sendBtnNodeList = root.findAccessibilityNodeInfosByText("发消息");
            if (sendBtnNodeList.size() > 0) {
                Log.e(TAG, "8.已经是好友无需添加,点击返回");
                performBackClick();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return;
            }
            String name =  findTextNodeInfoId("qj").getText().toString();
            if(mRecordMap.containsKey(name)){
                Log.e(TAG, "8.隊列中存在了 直接返回");
                performBackClick();
            }else {
                Log.e(TAG, "8.点击添加 添加到通讯录");
                mRecordMap.put(name,true);
                tagTextClick("添加到通讯录");
            }

        }
    }


    /**
     *
     * @param minAddTime
     * @param maxAddTime
     * @param name   搜索关键字的数组
     * @param text   打招呼文本
     */
    public void setParms(int minAddTime,int maxAddTime,List<String> name,String text){
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.text = text;
        this.names= name;

    }
}
